<?php

namespace App\Console\Command;


use App\Service\Mychat;
use DI\Annotation\Inject;
use Noodlehaus\Config;
use Predis\Client;
use Ratchet\ConnectionInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Wonfu\Core\AppConfig;
use Ratchet\WebSocket\WsServer as RatchetWsServer;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;

class WsServer extends Command
{

    protected static $defaultName = 'WsServer:run';

    /**
     * @Inject("appConfig")
     * @var AppConfig
     */
    private $appConfig;

    /**
     * @Inject
     * @var Config
     */
    private $envConfig;

    /**
     * @Inject()
     * @var Client
     */
    private $redis;


    protected function configure()
    {
        $this->setDescription('run websocket server.')
            ->addOption('port', 'p', InputOption::VALUE_OPTIONAL,
                'generate the openapi file', 8088)
            ->setHelp('This command allows you to run your websocket server');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $handler = new Mychat();
        $handler->setRedis($this->redis);
        $ws = new RatchetWsServer($handler);
        // Make sure you're running this as root
        $port = $input->getOption('port');
        $output->writeln("server will run in port {$port}");
        $server = IoServer::factory(new HttpServer($ws), $port);
        $server->run();

    }


}